package com.tsystems.school;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: victorn
 * Date: 8/21/11
 * Time: 6:48 PM
 * To change this template use File | Settings | File Templates.
 */
public class FileAnalyzer implements Runnable{

    private String filename;
    private String pattern;

    public FileAnalyzer(String file, String pattern){
        filename = file;
        this.pattern = pattern;
    }

    public void run() {
        if(analyze()){
            synchronized (System.out){
                System.out.println(filename);
            }
        }
    }

    private boolean analyze(){

        //System.out.println("analyze()");
        BufferedReader in;

        File f = new File(filename);

        try{

            if(f.isDirectory())
                return false;

            in = new BufferedReader(new FileReader(f));

            String str;

            while (in.ready()){
                str = in.readLine();

                if(str.contains(pattern))
                    return true;
            }

            in.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return false;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return false;
        }

        return false;

    }
}
